﻿#Region "Copyright"

' Copyright 2007 Ben Reichelt - http://benreichelt.net
' 
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
' 
'     http://www.apache.org/licenses/LICENSE-2.0
' 
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.


#End Region

Module Program

    Sub Main()

        If (Environment.GetCommandLineArgs().Length = 2 AndAlso _
            Environment.GetCommandLineArgs()(1) = "/?") _
            OrElse Environment.GetCommandLineArgs().Length = 1 Then
            PrintUsage()
            Return
        End If

        Dim context As New AppContext
        Dim parser As New CommandLineParser
        parser.Parse(Environment.GetCommandLineArgs(), context)
        context.Logger = New ConsoleLogger

        Dim metal As New SqlMetal(context)
        Dim metaData As XmlMetaData = metal.CreateXmlMetaDataFile()
        ExecuteCustomTasks(metaData, context)
        metal.CreateCodeFromXmlMetaData()
    End Sub

    Sub ExecuteCustomTasks(ByVal metaData As XmlMetaData, ByVal context As AppContext)
        ' Get all the custom tasks in source code
        Dim finder As New CustomTasks(context, New RealFileSystem)
        Dim tasks As List(Of IMetaDataTask) = finder.FindAll()

        ' execute them all
        For i As Integer = 0 To (tasks.Count - 1)
            tasks(i).Execute(metaData, context)
        Next

        metaData.Save()
    End Sub

    Private Sub PrintUsage()
        Console.WriteLine("usage: sqlplastic.exe")
        Console.WriteLine("/server:<ServerName>       - the database server name and database instance name")
        Console.WriteLine("/database:<Database>       - the name of the database")
        Console.WriteLine("/username:<UserName>       - the database username (leave blank for integrated authentication)")
        Console.WriteLine("/password:<Password>       - the database password (leave blank for integrated authentication)")
        Console.WriteLine("/outputxmlpath:<FilePath>  - the file path to save the LINQ to Sql DBML file (default is output.xml)")
        Console.WriteLine("/outputcodepath:<FilePath> - the file path to save the LINQ to Sql generated code (default is output.cs)")
        Console.WriteLine("/archivepath:<FilePath>    - the file path to archive the original, unaltered LINQ to Sql DBML file (default is output.original.xml)")
        Console.WriteLine("/namespace:<Namespace>     - the namespace to use in the generated classes")
        Console.WriteLine("/sqlmetallocation:<exeloc> - the location of SqlMetal.exe")
        Console.WriteLine("/language:<Language>       - the language to use to generate the code file, either 'vb' or 'cs' (default is 'cs')")
    End Sub

End Module
